﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Diagnostics;


namespace DictationInstaller
{
    [RunInstaller(true)]
    public partial class SlInstaller : Installer
    {
        public EventLog log;

        /// <summary>
        /// The origin uri the application refers for silent installation in future.
        /// </summary>
        public const string originUri = "http://localhost:61124/ClientBin/DictationToolSilverlight.xap";

        /// <summary>
        /// Returns the sllauncher path.
        /// </summary>
        private string Launcher
        {
            get
            {
                return string.Format("{0}\\Microsoft Silverlight\\sllauncher.exe", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
            }
        }

        /// <summary>
        /// Returns the downloaded XAP file physical path.
        /// </summary>
        private string XAP
        {
            get
            {
                string installDir = Context.Parameters["INSTALLDIR"];
                return string.Format("{0}{1}", installDir.Substring(0, installDir.Length - 1), "DictationToolSilverlight.xap");
            }
        }

        #region Overridden Events

        protected override void OnAfterInstall(IDictionary savedState)
        {
            if (!EventLog.SourceExists("MyApp"))
                EventLog.CreateEventSource("MyApp", "Application");

            log = new EventLog { Source = "MyApp" };
            Install();
            base.OnAfterInstall(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            Uninstall();
            base.Uninstall(savedState);
        }

        #endregion

        #region private methods goes here..

        /// <summary>
        /// Installs the silverlight application.
        /// </summary>
        private void Install()
        {
            try
            {
                string args = string.Format(@"/install:""{0}"" /origin:""{1}"" /shortcut:""desktop+startmenu"" /overwrite", XAP, originUri);
                StartProcess(args);
            }
            catch (Exception ex)
            {
                //Write into log.          
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Uninstalls the silverlight application.
        /// </summary>
        private void Uninstall()
        {
            try
            {
                string args = string.Format(@"/uninstall /origin:""{0}""", originUri);
                StartProcess(args);
            }
            catch (Exception ex)
            {
                //Write into log.   
                log.WriteEntry(ex.Message, EventLogEntryType.Error);
            }
        }

        /// <summary>
        /// Helper method that fires a new process for installing or uninstalling silverlight app.
        /// </summary>
        /// <param name="args"></param>
        private void StartProcess(string args)
        {
            var startInfo = new ProcessStartInfo
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                FileName = Launcher,
                Arguments = args
            };

            using (var process = Process.Start(startInfo))
            {
                process.StandardOutput.ReadToEnd();
            }
        }

        #endregion
    }
}
